package by.demchenko.sample.services.impl;

import by.demchenko.sample.dataaccess.base.ScheduleDao;
import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.services.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FacultyServiceImpl.class);

    @Inject
    private ScheduleDao dao;

    @Override
    public Schedule get(Integer id) {
        return dao.getById(id);
    }

    @Override
    @Transactional
    public void save(Schedule courseSchedule) {
        dao.insert(courseSchedule);
    }

    @Override
    public void saveOrUpdate(Schedule courseSchedule) {
        if (courseSchedule.getId() == null) {
            LOGGER.debug("Save new: {}", courseSchedule);
            dao.insert(courseSchedule);
        } else {
            LOGGER.debug("Update: {}", courseSchedule);
            dao.update(courseSchedule);
        }
    }

    @Override
    public void delete(Integer id) {
        dao.delete(id);
    }

    @Override
    public int getCount() {
        return dao.getCount();
    }

    @Override
    public List<Schedule> getAll() {
        return dao.findAll();
    }

    @Override
    public List<Schedule> getByTeacher(Person teacher) {
        return dao.findByTeacher(teacher);
    }

    @Override
    public int getCountByTeacher(Person teacher) {
        return dao.getCountByTeacher(teacher);
    }

    @Override
    public List<Person> getRegisteredStudents(Schedule schedule) {
        return dao.getRegisteredStudents(schedule);
    }

}